jQuery(document).on("pageinit", "[data-role='page'][data-pagetype='product']", function() {
    var page = jQuery(this);
    var prodid = get_url_params(page.jqmData("url"), "id");
    var url_product_content_swipeshow_items = URL_ACTION_NEWS_SWIPESHOW_ITEMS;
    var product_content_swipeshow_render = jQuery.extend(true, {}, swipeshow_render, {
        components:{
            page: page,
            slider: page.find("#slider"),
            slider_nav: page.find("#slider_nav"),
        },

        handler: function(items){
            swipeshow_render.handler.call(product_content_swipeshow_render, items);
        },
    });


    jQuery.getJSON(URL_ACTION_PROD_CONTENT, {"id": prodid}, function(result) {
        page.find("li h2.prodname").html(result.name)
            .end().find("div.price").html("￥"+result.discount+"<span>￥"+result.price+"</span>")
            .end().find("div.tel >a").attr("href", "tel:"+result.telephone).html(result.telephone)
            .end().find("div.addr").html("<b>地址: </b>"+result.address)
            .end().find("img.con_img").attr("src", URL_PREFIX + result.picture)
            .end().find("p.content").html(result.desc);
    });

    product_content_swipeshow_render.render(url_product_content_swipeshow_items, {});

    page.find("#prodMenu").find("a.favorite").click(function(event){
        $.ajax({
            type: "POST",
            url: URL_ACTION_PRODUCT_ADD_FAVOR,
            data: {
                product: prodid,
            },

            success: function(msg) {
                $.mobile.changePage(URL_VIEW_MY_PROD_FAVORS, {transition: "slide", reverse: false, changeHash: false });
            },

            error: function(req, status, error) {
                $.mobile.changePage(URL_VIEW_LOGIN, {transition: "slide", reverse: false, changeHash: false });
            }
        });        
    }).end().find("a.comment").click(function(event){
        $.mobile.changePage(URL_VIEW_PROD_COMMENT_ADD, {type: "get", data: {prodid: prodid}, transition: "slide", reverse: false, changeHash: false });
    });

    var _comments_render = this;
    var comments_listview_render = jQuery.extend(true, {}, listview_render, {
        components: {
            page: page,
            ul: page.find("div[data-role='content']").find("ul.comments-list")
        },

        gen_listview_item: function(item){
            return "<li data-itemid='"+item.id+"'>" +
                "<a href='#' data-transition='slide'>"+
                "<h2>"+item.nickname+"</h2>"+
                "<p>"+item.content+"</p>" +
                "<p class='ui-li-aside'><strong>" + item.createTime + "</strong></p>" + 
                "</a>"+
                "</li>";
        }
    });

    jQuery(".iscroll-wrapper", _comments_render).bind({
        "iscroll_onpullup"   :  function(event, data){
            if(comments_listview_render.pull_up){
                var params = {
                    product: prodid,
                    offset: comments_listview_render.components.ul.children("li:last").jqmData("itemid") || 0,
                    action: "up"
                };
                comments_listview_render.pull_up(URL_ACTION_PRODUCT_COMMENTS, params);
            }
        },
        "iscroll_onpulldown"   :  function(event, data){
            if(comments_listview_render.pull_down){
                var params = {
                    product: prodid,
                    offset: comments_listview_render.components.ul.children("li:first").jqmData("itemid") || 0,
                    action: "down"
                };
                comments_listview_render.pull_down(URL_ACTION_PRODUCT_COMMENTS, params);
            }
        }
    });

    comments_listview_render.render(URL_ACTION_PRODUCT_COMMENTS, {product:prodid});
});
